<?php

/**
 * 单元测试-数据库学生表增删改查
 *
 * @author peng.huang
 */
class DbCurdTest extends PHPUnit_Framework_TestCase {

    private $_dao;

    public function setUp() {
        $this->_dao = new common\dao\StudentDao();
    }

    /**
     * 新增
     */
    public function testAdd() {
        $insertID = $this->_dao->save(array('name' => '大冬瓜\'', 'age' => 3));
        $this->assertGreaterThan(-1, $insertID);
    }

    /**
     * 修改
     */
    public function testUpdate() {
        $affectedRows = $this->_dao->save(array('name' => '大冬瓜\'', 'age' => 5), 'name');
        $this->assertEquals(1, $affectedRows);
    }

    /**
     * 查询
     */
    public function testQuery() {
        $totalRecords = $this->_dao->count(array('name' => '大冬瓜\''));
        $this->assertEquals(1, $totalRecords);
    }

    /**
     * 删除
     */
    public function testDelete() {
        $affectedRows = $this->_dao->delete(array('name' => '大冬瓜\''));
        $this->assertEquals(1, $affectedRows);
    }

    /**
     * 事务
     */
    public function testTrans() {
        $sumAfeectRows = 0;
        $this->_dao->startTrans();
        $this->_dao->save(array('name' => '大冬瓜\'', 'age' => 1)) > -1 AND $sumAfeectRows ++;
        $this->_dao->save(array('name' => '大冬瓜\'', 'age' => 2)) > -1 AND $sumAfeectRows ++;
        $this->_dao->save(array('name' => '大冬瓜\'', 'age' => 3)) > -1 AND $sumAfeectRows ++;
        $this->_dao->completeTrans();
        $this->assertEquals(3, $sumAfeectRows);
    }

    /**
     * 批量插入
     */
    public function testBatchInsert() {
        $data = array();
        $range = range(1, 100);
        foreach ($range as $val) {
            $data[] = array('大冬瓜\'', $val);
        }

        $affectedRows = $this->_dao->batchInsert('name,age', $data, 10);
        $this->assertEquals(count($range), $affectedRows);
    }

    /**
     * 删除
     */
    public function _testDelete2() {
        $affectedRows = $this->_dao->delete(array('name' => '大冬瓜\''));
        $this->assertEquals(103, $affectedRows);
    }

}
